﻿using System;
using System.Collections.Generic;
using System.Text;
using taxi_sovereign.interfaces;

namespace taxi_sovereign.model
{
    [Serializable]
    class Passenger : interfaces.IPassenger 
    {
        //поля: место отправки(остановка), место прибытия(остановка), текущий транспорт(ITaxi) время счастливого ожидания (планируется ITime)

        interfaces.IStop Departure;
        interfaces.IStop Destination;
        interfaces.IVehicle CurrentTransport;
        int CurrentTimeWait = 0;
        int MaxTimeWait;
        
        _Constants._mood Mood;

        public Passenger(IStop departure, IStop destination, int maxtimewait)
        {
            Departure = departure;
            Destination = destination;
            MaxTimeWait = maxtimewait;
            Mood = _Constants._mood.HAPPY;
        }
        public Passenger() { Mood = _Constants._mood.HAPPY; }

       #region IPassenger Members

        private bool CheckTaxiRoute(interfaces.IVehicle TaxiToEnter)
        {
            return TaxiToEnter.Route().StopAvailable(Departure, Destination, TaxiToEnter.GlobalDirection);
        }

        private bool CheckEmptySpace(interfaces.IVehicle TaxiToEnter)
        {
            return TaxiToEnter.IsNotFull();
        }

        private void EnterTheTaxi(interfaces.IVehicle TaxiToEnter)
        {
            LeaveTheStop();
            this.CurrentTransport = TaxiToEnter;
            TaxiToEnter.AddPass(this);
        }

        private void LeaveTheStop()
        {
            Departure.DelPassesAdd(this);
        }

        private bool IsTooLongWait()
        {
            return (CurrentTimeWait > MaxTimeWait);
        }

        private void SetMood(_Constants._mood InputMood)
        {
            Mood = InputMood;
        }
        public void WaitTimeInc()
        {
            CurrentTimeWait++;
        }

        public int GetMood()
        {
            return (int)Mood;
        }

        public void ActionOnStop()
        {
            bool gone = false;
            List<IVehicle> vehicles = Departure.Vehicles();
            for (int i = 0; i < vehicles.Count; i++)
            {
                if (CheckTaxiRoute(vehicles[i]) && CheckEmptySpace(vehicles[i]))
                {
                    this.EnterTheTaxi(vehicles[i]);
                    gone = true;
                }
                if (gone) break;
            }
            if (!gone && IsTooLongWait())
            {
                SetMood(_Constants._mood.SAD);
            }
        }

        private void LeaveTheTaxi()
        {
            CurrentTransport.AddPassToDel(this);
        }

        public bool CurrentStopNeeded(interfaces.IStop CheckedStop)
        {
            return (CheckedStop == Destination);
        }

        public void ActionInTaxi(interfaces.IStop CurrentStop)
        {
            if (CurrentStopNeeded(CurrentStop)) LeaveTheTaxi();
        }
        public IStop destination
        {
            get { return Destination; }
            set { }
        }

        #endregion
    }
}
